/*
Functions:  Functions for manage memory allocation.
    Copyright (C) 2012  Jorge E. A. Atala
                        FuDePAN
    This file is part of the Fui Proyect.

    Fui is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Fui is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Fui.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef FUNCTIONS_H
#   define FUNCTIONS_H
#   include <cstdlib>
namespace fui
{

inline void* device_malloc(const size_t n)
{
    return malloc(n);
}


inline void device_free(void* ptr)
{
    free(ptr);
}

template<class T>
inline T* device_new(const size_t n = 1)
{
    return new T[n];
}

template<class T>
inline T* device_new(device_ptr<T>& ptr, const size_t n = 1)
{
    return device_new(ptr, T(), n);
}

template<class T>
inline T* device_new(device_ptr<T>& ptr, const T& exemplar, const size_t n = 1)
{
    //TODO When for_each algorithm be ready, will use here
    for (size_t counter = 0; counter < n; ++counter)
        new(ptr.get() + counter) T(exemplar);

    return ptr.get();
}

template<class T>
inline void device_delete(device_ptr<T>& ptr, const size_t n = 1)
{
    //TODO When for_each algorithm be ready, will use here
    for (size_t counter = 0; counter < n; ++counter)
        delete(ptr.get() + counter);
}


}
#endif

